#region Using

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;
using System.Globalization;
using BlogEngine.Core;
using BlogEngine.Core.Provider;
using BlogEngine.Core.Provider.Interfaces;

#endregion

namespace BlogEngine.Core.Provider.MSSQL
{
  /// <summary>
  /// Microsoft SQL Server Implementation of BlogProvider
  /// </summary>
	public class CILinks : ILinks
  {
    /// <summary>
    /// Gets a Link based on a Guid
    /// </summary>
    /// <param name="id">The link's Guid.</param>
    /// <returns>A matching Link</returns>
    public  Link Select(Guid id)
    {
        Link link = new Link();
        SQLManagedConnection myConn = SQLManagedConnection.GetConnection(Provider.DatabaseName);

        try
        {
            myConn.GetTextCommand("SELECT Title,Description, Website, Feed, XFNTag, Image, Blogroll, IsPublished, Slug FROM be_Links WHERE LinkID = @id");
            myConn.AddInParameter("@id", DbType.Guid, id);

            using (IDataReader rdr = myConn.ExecuteReader())
            {
                rdr.Read();

                link.Id = id;

                if (!rdr.IsDBNull(0))
                    link.Title = rdr.GetString(0);
                if (!rdr.IsDBNull(1))
                    link.Description = rdr.GetString(1);
                if (!rdr.IsDBNull(2))
                    link.Website = rdr.GetString(2);
                if (!rdr.IsDBNull(3))
                    link.Feed = rdr.GetString(3);
                if (!rdr.IsDBNull(4))
                    link.XFNTag = rdr.GetString(4);
                if (!rdr.IsDBNull(5))
                    link.Image = rdr.GetString(5);
                if (!rdr.IsDBNull(6))
                    link.BlogRoll = rdr.GetBoolean(6);
                if (!rdr.IsDBNull(7))
                    link.IsPublished = rdr.GetBoolean(7);
                if (!rdr.IsDBNull(8))
                    link.Slug = rdr.GetString(8);

                rdr.Close();
            }
        }
        finally
        {
            myConn.CloseIfNonTransactional();
        }

        return link;
    }

    /// <summary>
    /// Inserts a Link
    /// </summary>
    /// <param name="link">Must be a valid link object.</param>
    public  void Insert(Link link)
    {
        if (link == null)
            throw new ArgumentNullException("link");

        SQLManagedConnection myConn = new SQLManagedConnection(Provider.DatabaseName);

        try
        {
			myConn.GetTextCommand("INSERT INTO be_Links (LinkID, Title,Description, Website, Feed, XFNTag, Image, Blogroll, IsPublished, Slug) VALUES (@id, @title, @description, @website, @feed, @XFNTag, @Image, @IsPublished, @Slug)");
			myConn.AddInParameter("@id", DbType.Guid, link.Id); 
            myConn.AddInParameter("@title", DbType.String, link.Title);
            myConn.AddInParameter("@description", DbType.String, link.Description);
            myConn.AddInParameter("@website", DbType.String, link.Website);
            myConn.AddInParameter("@feed", DbType.String, link.Feed);
            myConn.AddInParameter("@XFNTag", DbType.String, link.XFNTag);
            myConn.AddInParameter("@Image", DbType.String, link.Image);
            myConn.AddInParameter("@Blogroll", DbType.Boolean, link.BlogRoll);
            myConn.AddInParameter("@IsPublished", DbType.Boolean, link.IsPublished);
            myConn.AddInParameter("@Slug", DbType.String, link.Slug);
            myConn.ExecuteNonQuery();

			if (!Link.Items.Contains(link))
				Link.Items.Add(link);

			Link.Items.Sort();            
        }
        finally
        {
            myConn.CloseIfNonTransactional();
        }
    }

    /// <summary>
    /// Updates a Link
    /// </summary>
    /// <param name="link">Must be a valid Link object.</param>
    public  void Update(Link link)
    {
        if (link == null)
            throw new ArgumentNullException("link");

        SQLManagedConnection myConn = new SQLManagedConnection(Provider.DatabaseName);

        try
        {
            myConn.GetTextCommand("UPDATE be_Link " +
                                  "SET Title = @title," +
                                  "Description = @description," +
                                  "Website = @website," + 
                                  "Feed = @feed," +
                                  "XFNTag = @XFNTag," +
                                  "Image = @Image," +
                                  "Blogroll = @Blogroll" +
                                  "IsPublished  = @IsPublished," +
                                  "Slug = @Slug" +
                                  "WHERE LinkID = @id");

			myConn.AddInParameter("@id", DbType.Guid, link.Id);
            myConn.AddInParameter("@title", DbType.String, link.Title);
            myConn.AddInParameter("@description", DbType.String, link.Description);
            myConn.AddInParameter("@website", DbType.String, link.Website);
            myConn.AddInParameter("@feed", DbType.String, link.Feed);
            myConn.AddInParameter("@XFNTag", DbType.String, link.XFNTag);
            myConn.AddInParameter("@Image", DbType.String, link.Image);
            myConn.AddInParameter("@Blogroll", DbType.Boolean, link.BlogRoll);
            myConn.AddInParameter("@IsPublished", DbType.Boolean, link.IsPublished);
            myConn.AddInParameter("@Slug", DbType.String, link.Slug); myConn.ExecuteNonQuery();

			if (!Link.Items.Contains(link))
				Link.Items.Add(link);

			Link.Items.Sort();

        }
        finally
        {
            myConn.CloseIfNonTransactional();
        }
    }

    /// <summary>
    /// Deletes a Link
    /// </summary>
    /// <param name="link">Must be a valid Link object.</param>
    public  void Delete(Link link)
    {
        if (link == null)
            throw new ArgumentNullException("link");

        SQLManagedConnection myConn = new SQLManagedConnection(Provider.DatabaseName);

        try
        {
			myConn.GetTextCommand("DELETE FROM be_link WHERE LinkID = @id");
            myConn.AddInParameter("@id", DbType.Guid, link.Id);
            myConn.ExecuteNonQuery();
		
			if (Link.Items.Contains(link))
				Link.Items.Remove(link);
		}
        finally
        {
            myConn.CloseIfNonTransactional();
        }
    }

    /// <summary>
    /// Fills a sorted list of Links.
    /// </summary>
    /// <returns>A List&lt;Link&gt; of all Links.</returns>
    public  List<Link> Fill()
    {
        List<Link> links = new List<Link>();

        //for each entry found, cause a data load to occur - done this way so the reader is shut first!
		foreach (Guid guid in SQLUtil.GetGuids("SELECT LinkID FROM be_Links", Provider.DatabaseName))
            links.Add(Link.Load(guid));

        links.Sort();
        return links;
    }
  }
}
